package openrr;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import openrr.GameMain;

//the renderer class deals with the rendering of the scene
public class Renderer {
	//windowHeight and windowWidth hold the dimensions of the game window
	int windowWidth = 640;
	int windowHeight = 480;
	GameMain game;
	
	//TODO: add a blit() method for 2D images
	
	//the constructor initializes the main game class
	public Renderer(int width, int height, GameMain gameMain)
	{
		windowWidth = width;
		windowHeight = height;
		game = gameMain;
	}
	
	//the render function is the function that is called from the main loop to render the scene.
	//it switches between 2 modes; 1 for 2D graphics and one for 3D graphics. 
	//I haven't used the 2D mode in the end, but initially I thought it would be nice to have some kind of simple interface for resetting or quitting the game, but that was a bit too much work.
	public void render()
	{
		//clear the scene, and put it in 3D mode
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
		set3DMode(windowWidth, windowHeight);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0, 0, -1*game.mapZoomLevel);
		glRotatef(game.mapPitch/5, 1, 0, 0);
		glRotatef(game.mapRotation, 0, 0, 1);
		glTranslatef(game.mapX, game.mapY, 0);
		glEnable(GL_TEXTURE_2D);
		
		//render the 3D scene
		game.displayManager.render();
		
		//switch to 2D mode
		set2DMode(windowWidth, windowHeight);
		//2D content goes here
		
	}
	
	//puts openGL in a 2D rendering mode
	private void set2DMode(int width, int height)
	{
		glDisable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluOrtho2D(0, width, 0, height);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}

	//switches openGL to 3D
	private void set3DMode(int width, int height)
	{
		glEnable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0f, (float)(width/height), 0.1f, 10000.0f);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}
}
